#!/usr/bin/env bash
# Usage: swiftenv local [<version>]
# Summary: Set or show the local Swift version
# completes

set -e

if [ "$1" = "--complete" ]; then
  echo "--unset"
  echo "system"
  exec swiftenv-versions --bare
fi

VERSION=$1
VERSION_FILE=".swift-version"

if [ "$VERSION" = "--unset" ]; then
  rm -f $VERSION_FILE
elif [ -z "$VERSION" ]; then
  if [ -f "$VERSION_FILE" ]; then
    cat "$VERSION_FILE"
  else
    echo "swiftenv: version is not set"
    exit 1
  fi
elif swiftenv-prefix "$VERSION" >/dev/null || [ "$VERSION" == "system" ]; then
  echo "$VERSION" > "$VERSION_FILE"
else
  echo "swiftenv: version \`$VERSION' is not installed"
  exit 1
fi
